﻿using Comm;
using Kinlo.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kinlo.Repo
{
    public class CounterRep
    {
        string strConn;
        public CounterRep()
        {
            strConn = ConfigurationManager.ConnectionStrings["WeightDB"].ConnectionString;
        }

        public int UpdateBackCounter(int outCount, int okCount, int ngCount)
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                {

                    var cmd = string.Format(@"UPDATE dbo.Counter  SET OutCount='{0}',OKCount='{1}',NGCount='{2}',LastTime=GETDATE()",
                    outCount, okCount, ngCount);
                    var sqlCmd = new SqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("{0} {1} {2}", "CounterRep", "UpdateBackCounter", ex.Message)); 
                return 0;
            }
        }

        public int UpdateFrontCounter(int inCount)
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                {

                    var cmd = string.Format(@"UPDATE dbo.Counter SET InCount='{0}',LastTime=GETDATE()", inCount);
                    var sqlCmd = new SqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("{0} {1} {2}", "CounterRep", "UpdateFrontCounter", ex.Message)); 
                return 0;
            }
        }

        /// <summary>
        /// 清零记数器
        /// </summary>
        /// <param name="outCount"></param>
        /// <param name="okCount"></param>
        /// <param name="ngCount"></param>
        /// <returns></returns>
        public int ClearCounter()
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                {
                    var cmd = string.Format(@"UPDATE dbo.Counter SET InCount='0',OutCount='0',OKCount='0',NGCount='0',LastTime=GETDATE()");
                    var sqlCmd = new SqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("{0} {1} {2}", "CounterRep", "ClearCounter", ex.Message)); 
                return 0;
            }
        }

        public Counter GetCounter()
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                {
                    var cmd = string.Format(@"SELECT TOP 1 ID,InCount,OutCount,OKCount,NGCount,LastTime FROM dbo.Counter  ORDER BY LastTime desc");
                    var sqlCmd = new SqlCommand(cmd, conn);
                    conn.Open();
                    var reader = sqlCmd.ExecuteReader();
                    return reader.ReaderToModel<Counter>();
                }
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("{0} {1} {2}", "CounterRep", "GetCounter", ex.Message)); 
                return null;
            } 
        }
        public bool GetDatabaseStatus()
        { 
            try
            {
                using (var conn = new SqlConnection(strConn))
                {
                    var cmd = string.Format(@"SELECT 1");
                    var sqlCmd = new SqlCommand(cmd, conn);
                    conn.Open();
                    var reader = sqlCmd.ExecuteReader();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

    }

}
